import Utils from '../../utils/utils';
import ToastUtil from '../../utils/toast';
import { Alert } from "react-native";
import moment from 'moment';

class OrderCreateMethods {
	// 判断是否有 授信账期
	_isSxzq = (orderType, payType) => {
		let result = false;
		if ((orderType == "S" && (payType == '3' || payType == '5')) || (orderType == "P" && (payType == '2' || payType == '4'))) {
			result = true;
		}
		return result;
	}

	// 验证 授信账期
	_sxzqValidation = (deliveryExpireDays, orderType) => {
		if (Utils.isEmpty(deliveryExpireDays)) {
			ToastUtil.showWarning(`请输入${orderType == 'P' ? '预付' : '赊销'}(授信)账期天数`);
		} else if (isNaN(deliveryExpireDays)) {
			ToastUtil.showWarning(`${orderType == 'P' ? '预付' : '赊销'}(授信)账期天数不是有效数字`);
		} else if (parseFloat(deliveryExpireDays) < 0) {
			ToastUtil.showWarning(`${orderType == 'P' ? '预付' : '赊销'}(授信)账期天数必须大于等于0`);
		}
	}

	//清空收付款类型
	_clearPayTypeData = (orderInfo) => {
		orderInfo.deliveryExpireDays = null;
		orderInfo.payExpireDays = null;
		orderInfo.prepayRate = null;
		return orderInfo;
	}

	////一级企业一级商品付款天数须小于等于3天
	_payExpireDays = (sendData, onoff) => {
		let { orderFlag, orderInfo, orderItemList } = sendData || {};
		let { companyCreditLevel, payExpireDays } = orderInfo || {};
		let { orderType, tCode } = orderFlag || {};
		let result = false;
		if (tCode != 'QH' && orderType == 'S' && companyCreditLevel == 1 && orderItemList && orderItemList.length && orderItemList.some(item => { return item.transLevel == 1; })) {
			if (payExpireDays && payExpireDays > 3) {
				if (onoff) {
					ToastUtil.showWarning('一级企业一级商品付款天数须小于等于3天');
				} else {
					Alert.alert('提示', '一级企业一级商品付款天数须小于等于3天', [
						{ text: '知道了', onPress: () => { console.log('确认') } }
					])
				}
				result = true;
			}
		}
		return result;
	}


	//【ID1004431】一级企业一级商品，选择交货期增加提示：一级企业一级商品交货起始时间提示
	_deliveryDataLimit = (creditLevelBuy, creditLevelSell, orderItemList) => {
		if ((creditLevelBuy == 1 || creditLevelSell == 1) && orderItemList && orderItemList.some(item => item.transLevel == 1)) return true;
		return false;
	}

	//禁止切换企业 条件: 敞口(包括转单) + 销售 +  货权转移 
	_switchEnterprise = (data) => {
		let { tCode, orderType, deliveryType } = data || {};
		if (tCode === 'S' && orderType === "S" && deliveryType === 'U') return true;
		return false;
	}

	// 优惠价 回调
	_callRebate = (callData, oldData) => {
		let { checkRebateFlag, rebateListItem, isPromotion } = callData || {};
		oldData.checkRebateFlag = checkRebateFlag;
		if (checkRebateFlag) {
			this._callRebateClear(oldData);
		} else {
			oldData.rebatePrice = rebateListItem ? rebateListItem.rebatePrice : null;
			oldData.regionCode = rebateListItem ? rebateListItem.regionCode : null;
			oldData.rebateId = rebateListItem ? rebateListItem.rebateId : null;
			oldData.isPromotion = Utils.isEmpty(isPromotion) ? null : isPromotion;
		}
		return oldData;
	}

	//清空优惠价 相关字段
	_callRebateClear = (oldData) => {
		oldData.rebatePrice = null;
		oldData.regionCode = null;
		oldData.rebateId = null;
		oldData.isPromotion = null;
	}

	_rebateValidation = (goodData) => {
		let { isPromotion, checkRebateFlag, rebateId } = goodData || {};
		let result = false;
		console.log("商品验证", goodData)
		if (isPromotion == 0 && !checkRebateFlag && Utils.isEmpty(rebateId)) { //
			result = true;
		}
		if (isPromotion == 1 && Utils.isEmpty(rebateId)) { // 如果是1必须使用,那么必须要有优惠价rebatePrice价格->rebateId
			result = true;
		}
		return result;
	}

	//尾款截止日期 时间转换   ["2021年","5月","11日"] => 2021-05-11
	_endPayTime = (timeArr) => {
		timeArr = timeArr && timeArr.map((item) => {
			let lsItem = item.slice(0, item.length - 1);
			lsItem = Utils.zeroPadding(lsItem);
			return lsItem;
		})
		return timeArr.join('-');
	}
	//交货日期和尾款截止日期校验(校验一个时间点是否在时间段内)
	_moreTime = (orderDeliveryPlanList, endTime) => {
		let minVal = false;
		let maxVal = false;
		if (orderDeliveryPlanList && orderDeliveryPlanList.length > 0 && !Utils.isEmpty(endTime)) {
			let endTimeDate = new Date(endTime);
			orderDeliveryPlanList.forEach((item) => {
				//最小值 -> 改成当前时间
				let startTime = moment().format('YYYY-MM-DD');
				if (!Utils.isEmpty(startTime)) {
					let minDate = new Date(startTime);
					if (minDate.getTime() <= endTimeDate.getTime()) minVal = true;
				}
				//最大值
				if (!Utils.isEmpty(item.deliveryEndDate)) {
					let maxDate = new Date(item.deliveryEndDate);
					if (maxDate.getTime() >= endTimeDate.getTime()) maxVal = true;
				}
			})
		}
		return (minVal && maxVal) ? true : false;
	}



}

module.exports = new OrderCreateMethods();